import os
import sys
import codecs
import json
import copy
import re
from typing import List, Dict, Tuple, Set, Union, Any, Optional


def merge_two_line_files(file1: str, file2: str, replace_dict: List[str]):
    with codecs.open(file1, 'r', 'utf-8') as f:
        file1_lines = f.readlines()
    with codecs.open(file2, 'r', 'utf-8') as f:
        file2_lines = f.readlines()
    # write final file with file1_lines and file2_lines
    with codecs.open('final_file.txt', 'w', 'utf-8') as f:
        for line1, line2, one_example in zip(file1_lines, file2_lines, replace_dict):
            line1 = line1.strip()
            line2 = line2.strip()
            f.write('tgt: {}\nsrc: {}\n$REPLACE: {}\n\n\n'.format(
                line1, line2, one_example))


def read_json_file(json_filename: str):
    if not os.path.exists(json_filename):
        raise FileExistsError('json_filename not exists')
    all_instances = list()
    with codecs.open(json_filename, 'r', 'utf-8') as f:
        for line in f.readlines():
            line = line.strip()
            all_instances.append(line)
    return all_instances


# start with main func()
if __name__ == '__main__':
    one_file = 'exp/gec_examples/tgt_shuf_100w.txt'
    two_file = 'exp/gec_examples/src_shuf_100w.txt'
    instance_file = 'exp/gec_examples/generated_exmaples_100w.shuf'
    instance_list = read_json_file(instance_file)
    merge_two_line_files(one_file, two_file, instance_list)
    print('merge two files done')
